import sys

from PySide6.QtCore import Qt, QRectF, QPointF
from PySide6.QtWidgets import QMainWindow, QDialog, QApplication

from 场景 import QDMGraphicsScene
from 指令节点 import QDMGraphicsNode
from 画布 import CanvasBase
from 窗体.主窗体 import Ui_MainWindow
from 窗体.测试窗口 import Ui_Form
from 节点插槽 import IN_Port, OUT_Port, ERROR_Port
from 节点连接线 import NodeEdge


# https://gitee.com/stdu-jtxy_liwx/PyFlow/tree/master/PyFlow/UI/Canvas


class Main_window(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        # 创建一个 QGraphicsView 实例
        original_graphics_view = self.graphicsView
        self.canvas = CanvasBase(sw=self)
        self.canvas.setObjectName("graphicsView")
        original_graphics_view.parent().layout().replaceWidget(original_graphics_view, self.canvas)
        original_graphics_view.deleteLater()
        # 创建一个 QGraphicsScene 场景
        self.scene = QDMGraphicsScene()
        self.scene.setSceneRect(QRectF(0, 0, 5000, 5000))
        self.canvas.setScene(self.scene)
        # 节点和边的列表
        self.nodes = []
        self.edges = []

    def open_test(self, drag_position):
        # 打印self.treeWidget选中的项的文本
        print('选中的项:', self.treeWidget.currentItem().text(0))
        print('拖放位置:', drag_position)
        test = Test(parent=self, scene=self.canvas.scene(), drag_position=drag_position)
        test.setModal(True)
        test.exec()

    def keyPressEvent(self, event):
        if event.key() == Qt.Key.Key_Delete:
            self.delete_selected_items()  # 删除选中的项

    def delete_selected_items(self):
        """在场景中删除选中的项"""
        selected_items = self.canvas.scene().selectedItems()
        if selected_items:
            for item in selected_items:
                print('删除的项:', item)
                self.canvas.scene().removeItem(item)

    def add_graph_node(self, drag_position):
        """添加图形节点
        :param drag_position: 放置位置
        """
        in_port = IN_Port()  # 输入插槽
        ex_port = OUT_Port()  # 输出插槽
        error_port = ERROR_Port()  # 错误插槽
        # 在拖放位置添加一个自定义的节点
        node = QDMGraphicsNode(
            self.scene, 0, "Custom Info", "Remarks",
        )
        node.add_port(in_port)  # 添加一个插槽
        node.add_port(ex_port, position='右下')
        node.add_port(error_port, position='右中')
        # 设置节点的位置，中心点为拖放位置
        node.setPos(drag_position.x() - node.width / 2, drag_position.y() - node.height / 2)
        self.scene.addItem(node)
        self.nodes.append(node)  # 添加到节点列表

    def add_node_edge(self, source_port, des_port):
        """添加节点边
        :param source_port: 起始插槽
        :param des_port: 终止插槽
        """
        edge = NodeEdge(source_port, des_port, self.scene)
        edge.update_edge_path()
        self.edges.append(edge)  # 添加到边列表


class Test(QDialog, Ui_Form):
    def __init__(self, parent=None, scene=None, drag_position=None):
        super().__init__(parent)
        self.setupUi(self)
        self._node = None
        self.scene = scene  # 传递场景
        self.drag_position = drag_position  # 传递拖放位置
        self.pushButton.clicked.connect(self.test_func)

    def test_func(self):
        self.parent().add_graph_node(self.drag_position)  # 添加图形节点
        # 位置偏移，再添加一个图形节点
        self.parent().add_graph_node(self.drag_position + QPointF(300, 300))
        # 添加节点边
        self.parent().add_node_edge(
            self.parent().nodes[0].out_port,
            self.parent().nodes[1].in_port
        )
        self.close()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = Main_window()
    main_window.show()
    sys.exit(app.exec())
